package com.cloud.animationtestdemo.strage

import android.animation.ArgbEvaluator
import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.view.View

/**
 * @author: njb
 * @date:   2025/1/1 23:47
 * @desc:   描述
 */
class EvaluatorAnimationStrategy:BaseAnimationStrategy {
    private val argbProperty = PropertyValuesHolder.ofObject("argb", ArgbEvaluator(), 0, 0)

    override fun getAlphaForProgress(progress: Float): Float {
        // 根据进度自定义透明度变化，比如进度过半后透明度降低
        return if (progress < 0.5f) progress * 1.2f else (1 - progress) * 1.2f
    }

    fun animateView(view: View, startColor: Int, endColor: Int, duration: Long) {
        argbProperty.setObjectValues(startColor, endColor)

        val animator = ValueAnimator.ofPropertyValuesHolder(argbProperty)
        animator.duration = duration
        animator.addUpdateListener { animation ->
            val animatedValue = animation.animatedValue as? Int
            val currentColor = animatedValue?: startColor
            view.setBackgroundColor(currentColor)
            val progress = animation.animatedFraction
            val alpha = getAlphaForProgress(progress)
            view.alpha = alpha
        }
        animator.start()
    }
}